package com.chenxi;

import java.util.Arrays;
import java.util.concurrent.locks.ReentrantLock;
/*
getQueueLength()方法的作用是返回正等待获取此锁定的线程数。
 */

public class MyQueueLen {
    public ReentrantLock lock = new ReentrantLock();

    public void serviceMe(){
        try {
            lock.lock();
            System.out.println("ThreadName = "+ Thread.currentThread().getName() + "获取锁");
            Thread.sleep(Integer.MAX_VALUE);
        }catch (InterruptedException e){
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyQueueLen myQueueLen = new MyQueueLen();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                myQueueLen.serviceMe();
            }
        };
        Thread[] threads = new Thread[10];
        for (int i = 0;i<10;i++){
            threads[i] = new Thread(runnable);
        }
        Arrays.stream(threads).forEach(thread -> thread.start());
        Thread.sleep(1000);
        System.out.println("有" + myQueueLen.lock.getQueueLength() + "个线程在等待锁");
    }
}
